<template>
  <el-container class="home_container">
    <!-- =============header区域================ -->
    <el-header class="home_header">
      <el-button
        class="quitBtn"
        icon="el-icon-error"
        circle
        @click="homeQuit"
      ></el-button>
      <div class="home_header_index">
        <img src="../assets/jiluo.jpg" />
        <span>后台管理系统</span>
      </div>
    </el-header>
    <!-- ======================header区域结束==================== -->
    <el-container>
      <!-- ================侧边栏区域================= -->
      <el-aside :width="collapseclk ? '64px' : '200px'" class="home_aside">
        <div class="toggle" @click="toggleHide">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="collapseclk"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
            default-active="2"
            active-text-color="#ffd04b"
          >
            <template slot="title">
              <i :class="icons[item.id + '']"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item-group
              default-active="2"
              :index="childItem.id + ''"
              v-for="childItem in item.children"
              :key="childItem.id"
            >
              <el-menu-item
                :index="'/' + childItem.path"
                @click="saveNameState('/' + childItem.path)"
              >
                <i class="el-icon-menu"></i>
                <span>{{ childItem.authName }}</span>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- ================侧边栏区域结束=============== -->

      <!-- ================主界面区域================ -->
      <el-main class="home_main">
        <router-view></router-view>
      </el-main>
      <!-- ================主界面结束================ -->
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      menuList: [],
      icons: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      collapseclk: false,
      activePath: ''
    }
  },
  methods: {
    homeQuit () {
      window.sessionStorage.clear()
      this.$router.push('./login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status === 200) {
        this.menuList = res.data
      } else {
        return this.$message.error(res.meta.msg)
      }
    },
    // 点击按钮切换菜单的折叠与展开
    toggleHide () {
      this.collapseclk = !this.collapseclk
    },
    // 解决二级标签的高亮问题
    saveNameState (val) {
      window.sessionStorage.setItem('pathName', val)
      this.activePath = val
    }
  },
  // 在创建时就请求菜单数据
  created () {
    console.log(this)
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('pathName')
  }
}
</script>
<style lang="less" scoped>
.home_container {
  height: 100%;
}

// ====================header区域=======================
.home_header {
  position: relative;
  background-color: #373d41;
}

.home_header_index > img {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  display: block;
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

.home_header_index > span {
  color: #ccc;
  font-size: 28px;
  line-height: 60px;
  margin-left: 50px;
}

.el-button.is-circle {
  padding: 0;
}

.el-button {
  background-color: #f00;
}

.quitBtn {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: scale(1.8) translateY(-50%);
  border: 0px;
  background-color: #fff;
  padding: 0;
}

// ===================header区域结束=====================

// ============侧边栏区域==================
.home_aside {
  background-color: #333744;
}

.home_aside > .toggle {
  width: 100%;
  height: 24px;
  color: #fff;
  text-align: center;
  line-height: 24px;
  font-size: 12px;
  letter-spacing: 0.1rem;
  cursor: pointer;
  background-color: #335077;
}

.home_aside > .el-menu {
  border-right: none;
}
// ==========侧边栏区域结束============
.home_main {
  background-color: #cdf;
}

.iconfont {
  margin-right: 10px;
}
</style>
